<?php
class Pitch extends AppModel {
	var $name = 'Pitch';
	var $validate = array(
		'user_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'title' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'deadline' => array(
			'date' => array(
				'rule' => array('date'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'design_count' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	var $hasMany = array(
		'Design' => array(
			'className' => 'Design',
			'foreignKey' => 'pitch_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'PitchSubscription' => array(
			'className' => 'PitchSubscription',
			'foreignKey' => 'pitch_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
	
	var $actsAs = array('Containable');
	
	// find 5 random pitches
	function get_random() {
		$this->recursive = 3;
		$data = $this->find('all', array('contain' => array('User'), 'fields' => array('Pitch.category', 'Pitch.deadline', 'Pitch.reward', 'Pitch.id')));
		$i = 0;
		// find related Contractor profiles
		foreach($data as $pitch) {
			$avatar = $this->User->ContractorProfile->find('first', array('conditions' => array('ContractorProfile.user_id' => $pitch['User']['id']), 'fields' => array('ContractorProfile.avatar')));
			$data[$i]['ContractorProfile']['avatar'] = $avatar['ContractorProfile']['avatar'];
			$i++;
		}
		shuffle($data);
		$data = array_slice($data, 0, 5);
		return $data;
	}
	
	// find all pitches
	function get_pitches($filter = 'all') {
		// check if user is contractor
		if ($_SESSION['Auth']['User']['group_id'] == 2) {
			// find all pitches that belong to current user
			$pitches = $this->findMy('all');
			$i = 0;
			// find related Contractor profiles
			foreach($pitches as $pitch) {
				$profile = $this->User->ContractorProfile->find('first', array('conditions' => array('ContractorProfile.user_id' => $pitch['Pitch']['user_id'])));
				$pitches[$i] = array_merge($pitch, $profile);
				$i++;
			}
			return $pitches;
		} else {
			// check if filter is set
			if ($filter == 'own') {
				// if yes, get all pitches where user subscribed to
				$pitches = $this->PitchSubscription->find_subscribed($_SESSION['Auth']['User']['id'], false);
			} else {
				// if nu, find all
				$pitches = $this->find('all');
			}
			$i = 0;
			// find related Contractor profiles
			foreach($pitches as $pitch) {
				$profile = $this->User->ContractorProfile->find('first', array('conditions' => array('ContractorProfile.user_id' => $pitch['Pitch']['user_id'])));
				$pitches[$i] = array_merge($pitch, $profile);
				$i++;
			}
			return $pitches;
		}
	}
	
	// find one pitch
	function get_pitch($id = null) {
		if ($id == null) {
			return false;
		} else {
			$conditions = array(
				'Pitch.id' => $id
			);
			// if user is contractor make sure he only finds his own pitches
			if ($_SESSION['Auth']['User']['group_id'] == 2) {
				$secondconditions = array(
					'Pitch.user_id' => $_SESSION['Auth']['User']['id']
				);
				$conditions = array_merge($conditions, $secondconditions);
			}
			// find pitch
			$pitch = $this->find('first', array('conditions' => $conditions));
			// find related contractor profile
			$profile = $this->User->ContractorProfile->find('first', array('conditions' => array('ContractorProfile.user_id' => $pitch['Pitch']['user_id'])));
			// merge data
			$pitch = array_merge($pitch, $profile);
			// check if locked and if user has acces
			if ($pitch['Pitch']['locked'] == 1) {
				if ($_SESSION['Auth']['User']['group_id'] == 2) {
					return $pitch;
				} else {
					$conditions = array(
						'PitchSubscription.user_id' => $_SESSION['Auth']['User']['id'],
						'PitchSubscription.pitch_id' => $pitch['Pitch']['id']
					);
					$check = $this->PitchSubscription->find('first', array('conditions' => $conditions));
					if ($check) {
						return $pitch;
					} else {
						return false;
					}
				}
			} else {
				return $pitch;
			}
		}
	}
	
	// get pitches update
	function get_updates() {
		// check if user is contractor
		if ($_SESSION['Auth']['User']['group_id'] == 2) {
			$conditions = array(
				'Pitch.user_id' => $_SESSION['Auth']['User']['id'],
			);
			$pitches = $this->find('all', array('conditions' => $conditions, 'order' => array('Pitch.created' => 'DESC'), 'limit' => 10));
		} else {
			$contain = array(
				'User' => array(
					'ContractorProfile'
				),
				'Design'
			);
			$pitches = $this->find('all', array('order' => array('Pitch.created' => 'DESC'), 'limit' => 10, 'contain' => $contain));
		}
		return $pitches;
	}

}
?>
